package com.gitpull.service.serviceImpl;

import com.gitpull.mapper.excelMapper;
import com.gitpull.pojo.gitInfo;
import com.gitpull.pojo.student;
import com.gitpull.service.excelService;
import com.gitpull.utils.GitUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class excelServiceImpl implements excelService {

    private static final String PRIVATE_TOKEN = "27afe4f19ba5d1b8e2310c206f7055a5";

    @Autowired
    private excelMapper excelMapper;

    @SneakyThrows
    @Override
    @Transactional
    public void saveExcel(List<student> userList) {
        // 删除现有表，如果存在
        excelMapper.dropStudentTableIfExists();
        excelMapper.dropGitInfoTableIfExists();

        // 创建新表
        excelMapper.createStudentTable();
        excelMapper.createGitInfoTable();

        // 保存学生信息到数据库
        excelMapper.saveExcel(userList);

        // 遍历学生列表
        for (student student : userList) {
            try {
                String warehouseAddress = student.getWarehouseAddress();
                String studentNumber = student.getStudentNumber();
                List<gitInfo> gitInfos = GitUtils.LogRemote(warehouseAddress, PRIVATE_TOKEN, studentNumber);
                excelMapper.saveExcelInfo(gitInfos);
            } catch (Exception e) {
                // 记录错误信息并继续下一个学生
                System.err.println("连接仓库失败: " + student.getStudentNumber());
                continue; // 继续处理下一个学生
            }
        }
    }
}
